package mdnsapp;

import java.io.FileWriter;
import java.net.InetAddress;
import org.jdom.Attribute;
import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class ConfigFileEdit {

    public Document doc;
    FileWriter file;
    public XMLOutputter outputter;
    String fileName;

    public ConfigFileEdit(String fileName) {
        try {

            SAXBuilder builder = new SAXBuilder();
            this.fileName = fileName;
            doc = builder.build(fileName);

        } catch (Exception ex) {
            System.out.println("Exception in Contructor of ConfigFileEdit is " + ex.getMessage());
        }
    }

    Element getRoot() {
        return this.doc.getRootElement();
    }

    Element setRoot(String root) {
        Element rootElement = new Element(root);
        doc.setRootElement(rootElement);
        return rootElement;
    }

    void setAttribute(Element element, String name, String value) {
        Attribute attr = new Attribute(name, value);
        element.setAttribute(attr);
    }

    void addContent(Element element, String content) {
        element.addContent(content);
    }

    Element setChild(Element element, String name) {
        Content content = new Element(name);
        element.setContent(content);
        return (Element) content;
    }

    Element getInitArgsChild() {
        return this.doc.getRootElement().getChild("global").getChild("initargs");
    }
    Element getGatewayChild()
    {
    return this.doc.getRootElement().getChild("stages").getChild("Router").getChild("initargs");
    }

    public boolean writeSupernodeIP_cfg(String Ip, String Port) {
        Element con = getInitArgsChild();
        Element gateway = getGatewayChild();
        String newText =
                "\ndebug_level  0\n" +
                "gateway	"+Ip+":"+Port+"\n"+
                "#periodic_ping_period	20\n" +
                "#ls_alarm_period     	4 \n" +
                "#near_rt_alarm_period  	0 \n" +
                "#far_rt_alarm_period 	10 \n" +                "leaf_set_size          1 \n" +
                "digit_values		2 \n" +
                "#ignore_proximity	false \n" +
                "location_cache_size     0 \n";


        con.removeContent();
        gateway.removeContent();
        InetAddress local = null;
        try {
            local = InetAddress.getLocalHost();
        } catch (Exception ex) {
        }
        System.err.println("local = " + local.getHostAddress());
        con.addContent("node_id  " + local.getHostAddress() + ":" + Port + "\n");
        gateway.addContent(newText);
        write();
        return true;
    }

    void write() {
        try {

            file = new FileWriter(this.fileName);
            outputter = new XMLOutputter();
            outputter.output(doc, file);
            System.out.println("DHT Configuration file is ready..Now you can start connecting..");
            file.close();
        } catch (Exception ex) {

            System.out.println("Exception in ConfigFileEdit.write() method is " + ex.getMessage());
        }
    }

    public static void main(String[] check) {
        ConfigFileEdit tmpFile = new ConfigFileEdit("dht.cfg");
        System.err.println(" Root is " + tmpFile.getRoot());
        tmpFile.writeSupernodeIP_cfg("11.11.11.11", "5353");



    }
}
